package util;

import partParsers.AdvData;

//This class allows to find html text between tags and check if it is one of
//known real estate data types.
//
public class HtmlTextFinder extends AdvData{	
//Class attributes
	//protected int curTagPos=0;//position of last <td> tag in curStr
	protected int curPos=0;//current position of in curStr - after last found
	protected int curValPos=0;//position before last name found
	protected String curStr=null;//last buffer string read
	
//Class methods
	//Constructor that must be used by default		
	public HtmlTextFinder(String buf)
	{
		curStr= buf==null ? null : buf;
	}

		
	//Method returns html text from curStr if exist, otherwise retuns null;	
	public String getHtmlText(){return getHtmlText(curPos);}
	
	//Method returns html text from curStr if exist, otherwise retuns null;
	public String getHtmlText(int fromIndex)
	{
		//check parameters correctness
		if(curStr==null)
			return null;		
		if(fromIndex<0)
			curPos=fromIndex=curStr.length();
			
		if(fromIndex>=curStr.length())
			return null;

		//look for text between html tags
		String sFound= curStr.substring(fromIndex),sTemp;
		
		int i=0,j=0;
		sTemp=sFound;
		while(i<sTemp.length())
		{
			while(i<sTemp.length() && sTemp.charAt(i)!='<' ) {
                i++;
            }
			if(j<=i)
			{
				sFound=sTemp.substring(j,i).trim();
				if(!sFound.equals(""))
					break;
			}
			while(i<sTemp.length() && sTemp.charAt(i)!='>') {
                i++;
            }
			j=i+1;
			
		}
		
		sFound=sFound.replaceAll("&NBSP;","");
		sFound=sFound.replaceAll("&NBSP","");
		sFound=sFound.replaceAll("&QUOT;","");
		sFound=sFound.replaceAll("&QUOT","");			
		sFound=sFound.trim();			
		
		curPos=fromIndex+i;
		curValPos=fromIndex+j;
		
		if(sFound==null || sFound.equals("") || j>i)
			return null;
		else
			return sFound;
	
	}
	
	//	Method returns type of first data title that param string contains 	
	public int getType(){return getType(curPos);}

	//returns type of 'str' or -1
	public int getTypeOf(String str)
	{
		String sFound=str;
		boolean bFound=false;
		int i,j;
		
		sFound=sFound.replaceAll("&NBSP;","");
		sFound=sFound.replaceAll("&NBSP","");
		sFound=sFound.replaceAll("&QUOT;","");
		sFound=sFound.replaceAll("&QUOT","");			
		sFound=sFound.trim();			
		
		
		//check if text found
		for(i=0;sFound!=null && i<ADV_DATA_SIZE;i++)
		{
			j=0;
			for(;j<advDataNames[i].length && !bFound;j++)
				if(sFound.equals(advDataNames[i][j]) ||
                   sFound.startsWith(advDataNames[i][j]+":") ||
                   sFound.startsWith(advDataNames[i][j]+" :"))
				{
					bFound=true;
					break;
				}
			if(bFound)
				break;				
		}		
		
		if(!bFound)
			i=-1;
		
		return i;
	}
	
	//Method returns type of first data title that param string contains 
	//after k position
	public int getType(int k)
	{
		if(curStr==null)
			return -1;

		if(k<0)
			k=0;

		int i,j=-1;
		String sFound;
		boolean bFound=false;
				
		if(curPos<0)
			return -1;			
		
		sFound=getHtmlText();//find next html text
		
		if(sFound==null)
			return -1;
		
		//check if text found
		for(i=0;i<ADV_DATA_SIZE;i++)
		{
			j=0;
			for(;j<advDataNames[i].length && !bFound;j++)
				if(sFound.equals(advDataNames[i][j]) ||
                   sFound.startsWith(advDataNames[i][j]+":") ||
                   sFound.startsWith(advDataNames[i][j]+" :"))
				{
					bFound=true;
					break;
				}
			if(bFound)
				break;				
		}				

		//move position after text found
		if(bFound)
		{
			curPos=
				curStr.indexOf(advDataNames[i][j],curValPos)+
				advDataNames[i][j].length();
			if(curPos>=0 && curPos<curStr.length() && curStr.charAt(curPos)==':')
				curPos++;
			else
				if(curPos>=0 && curPos+1<curStr.length() && 
				   curStr.charAt(curPos)==' ' && curStr.charAt(curPos+1)==':')
					curPos+=2;						    								
		}
			
		return bFound ? i : -1;
	}
	
	//move finder current position to index 'i'.
	//if position 'i' is out of the buffer move to the end of buffer.
	//if position is negative move to the start of buffer.
	public void moveTo(int i)
	{
		if(i<0)
			curPos=0;
		else
			if(i>=curStr.length())
				curPos=curStr.length();
			else curPos=i;
	}
	
	//move finder to the 'end-of-search' state
	public void moveToEnd(){curPos=curStr.length();}
	
	//true if finder in the 'end-of-search' state
	public boolean hasNext(){return (curPos<curStr.length() && curPos>=0);}
	
	//returns current position in buffer
	public int getPos(){return curPos;}

	//returns position of last value found in buffer
	public int getValPos(){return curValPos;}

	//Method returns buffer length
	public int getBufLen(){return curStr==null ? 0 : curStr.length();}
	
	//returns copy of buffer string
	public String getBuffer(){return curStr==null ? null : curStr;}
	
	//returns first index of 'str' after curPos in buffer of finder
	public int indexOf(String str){return curStr.indexOf(str,curPos);}

	//returns first index of 'str' after curPos in buffer of finder
	public int indexOf(String str,int pos){return curStr.indexOf(str,pos);}
	
	//returns substring of buffer
	public String substring(int pos){return curStr.substring(pos);}
	public String substring(int pos,int pos2){return curStr.substring(pos,pos2);}
	
	//returns character at position i in buffer
	public int charAt(int pos){return curStr.charAt(pos);}
}
